package com.j180.dao.detail;

import com.j180.bean.Detail;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 明细的dao层
 */
public interface IDetailDao {

    /**
     * 查询所以明细
     * @return  返回一个detail对象的list集合
     */
    @Select("select " +
            " det.d_id as did," +
            " sto.stg_id as stid," +
            " det.d_gNum as gnum," +
            " ser.se_id as seid," +
            " det.d_sNum as snum," +
            " ord.o_id as oid from t_detail as det " +
            " left join t_stogoods as sto on det.d_stg_id=sto.stg_id" +
            " left join t_service as ser on det.d_se_id=ser.se_id" +
            " left join t_order as ord on det.d_o_id=ord.o_id")
    @ResultMap("mapper.detail.detailMap")
    public List<Detail> getDetailAll();

    /**
     * 查询一个明细
     * @param id 明细id
     * @return 返回一个detail对象
     */
    @Select("select " +
            " det.d_id as did," +
            " sto.stg_id as stid," +
            " det.d_gNum as gnum," +
            " ser.se_id as  seid," +
            " det.d_sNum as snum," +
            " ord.o_id as oid from t_detail as det " +
            " left join t_stogoods as sto on det.d_stg_id=sto.stg_id" +
            " left join t_service as ser on det.d_se_id=ser.se_id" +
            " left join t_order as ord on det.d_o_id=ord.o_id where det.d_id=#{id}")
    @ResultMap("mapper.detail.detailMap")
    public Detail getDetailById(@Param("id") int id);

    /**
     * 添加明细
     * @param detail 传入的对象
     * @return  返回一个加入数据库的条数
     */
    @Insert("insert into t_detail values(null," +
            "#{stoGoods.id}," +
            "#{gNum}," +
            "#{service.id}," +
            "#{sNum}," +
            "#{order.id})")
    public int insertDetail(Detail detail);

    /**
     * 修改明细
     * @param detail 传入的对象
     * @return  返回一个修改数据库的条数
     */
    @Update("update t_detail set " +
            " d_stg_id=#{stoGoods.id}," +
            " d_gNum=#{gNum}," +
            " d_se_id=#{service.id}," +
            " d_sNum=#{sNum}," +
            " d_o_id=#{order.id}" +
            " where d_id = #{id}")
    public int upDetail(Detail detail);

    /**
     * 删除明细
      * @param id 明细的id
     * @return  返回一个删除数据库的条数
     */
    @Delete("DELETE from  t_detail as det where det.d_id=#{id} ")
    public int delDetailById(@Param("id") int id);

}
